﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Nature.Text
{
    public static class Extensions
    {
        public static Match Match(this Regex rgx, TextSource source, int index, int length)
        {
            return rgx.Match(source.AdaptedText, index, length);
        }

        [DebuggerNonUserCode()]
        public static Match Match(this Regex rgx, TextSource source, Capture capture)
        {
            return rgx.Match(source.AdaptedText, capture.Index, capture.Length);
        }

        public static bool IsMatch(this Regex rgx, TextFragment fragment)
        {
            return rgx.IsMatch(fragment.AdaptedText);
        }

        public static Match Match(this Regex rgx, TextFragment fragment)
        {
            return rgx.Match(fragment.Source.AdaptedText, fragment.Index, fragment.Length);
        }

        public static IEnumerable<Match> Matches(this Regex rgx, TextFragment fragment)
        {
            List<Match> matches = new List<Match>();
            for (Match mch = rgx.Match(fragment.Source.AdaptedText, fragment.Index, fragment.Length); mch.Success; mch = mch.NextMatch())
            {
                matches.Add(mch);
            }
            return matches;
        }

        public static void Submit(this ITextInfoEventListener listener, 
            TextInfoType infoType, string message, TextSource source, Capture capture)
        {
            listener.Submit( new TextInfoEventArgs(infoType, message, new TextFragment(source, capture)));
        }

        public static void Submit(this ITextInfoEventListener listener,
            TextInfoType infoType, string message, TextFragment fragment)
        {
            listener.Submit(new TextInfoEventArgs(infoType, message, fragment));
        }

        public static void SubmitError(this ITextInfoEventListener listener,
            TextInfoType infoType, string message, TextSource source, Capture capture)
        {
            listener.Submit(new TextInfoEventArgs(TextInfoType.Error, message, new TextFragment(source, capture)));
        }

        public static void SubmitError(this ITextInfoEventListener listener,
            TextInfoType infoType, string message, TextFragment fragment)
        {
            listener.Submit(new TextInfoEventArgs(TextInfoType.Error, message, fragment));
        }
    }
}
